package com.huang825172.sbog.Tabs.Mine.InfoEditPage.Store;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.huang825172.sbog.BaseFlux.Action.BaseAction;
import com.huang825172.sbog.BaseFlux.Store.BaseStore;
import com.huang825172.sbog.GlobalManager.LoginStateManager;
import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.Mine.InfoEditPage.Action.PhotoSelectAction;
import com.huang825172.sbog.Tabs.Mine.InfoEditPage.Action.SubmitAction;
import com.huang825172.sbog.Tabs.Mine.InfoEditPage.Store.Datas.editInfo;
import com.huang825172.sbog.Tabs.Mine.InfoEditPage.Store.Datas.editInfoRes;
import com.huang825172.sbog.Util.TAGs;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class Store extends BaseStore {

    private LoginStateManager manager;
    private String photoFilePath;

    public Store(Context context) {
        super(context);
        manager = new LoginStateManager(context);
    }

    @Override
    public List<String> getActionLabels() {
        List<String> labels = new ArrayList<>();
        labels.add("InfoEditPageSubmitAction");
        labels.add("InfoEditPagePhotoSelectAction");
        return labels;
    }

    @Override
    public void changeData(BaseAction action) {
        super.changeData(action);
        switch (action.getLabel()) {
            case "InfoEditPageSubmitAction": {
                if (manager.getLoginPhone() != null || manager.getLoginToken() != null) {
                    SubmitAction act = (SubmitAction) action;
                    editInfo info = new editInfo(
                            manager.getLoginToken(),
                            manager.getLoginPhone(),
                            act.getUsername(),
                            act.getEmail(),
                            act.getGender(),
                            act.getLocation(),
                            act.getComment(),
                            act.getAge()
                    );
                    EditInfo(info);
                }
                break;
            }
            case "InfoEditPagePhotoSelectAction": {
                PhotoSelectAction act = (PhotoSelectAction) action;
                this.photoFilePath = act.getFilePath();
                notifyDataChanged(act.getLabel());
            }
            default:
                break;
        }
    }

    private void EditInfo(editInfo info) {
        final Gson gson = new Gson();
        final String infoStr = gson.toJson(info);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(context.getString(R.string.server_baseUrl))
                .build();
        Interfaces request = retrofit.create(Interfaces.class);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), infoStr);
        Call<ResponseBody> call = request.editInfo(body);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String resStr = response.body().string();
                    editInfoRes resObj = gson.fromJson(resStr, editInfoRes.class);
                    if (resObj.code == 200) {
                        notifyDataChanged("InfoEditPageEditSuccess");
                    }
                    Log.d(TAGs.INFO, infoStr);
                    Log.d(TAGs.INFO, resObj.msg);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(TAGs.ERROR, e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
                Log.e(TAGs.ERROR, t.getMessage());
            }
        });
    }

    public String getPhotoFilePath() {
        return photoFilePath;
    }
}
